package br.com.biavan.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.biavan.model.Conteudo;
import br.com.biavan.utils.HibernateUtil;

public class ConteudoDAO {
	
	private Session session;		
			
	public boolean inserir(Conteudo conteudo) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(conteudo);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public boolean atualizar(Conteudo conteudo) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(conteudo);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public boolean apagar(Conteudo conteudo) {
		boolean resp = false;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(conteudo);
			session.getTransaction().commit();
			resp = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return resp;
	}

	public List<Conteudo> listar() {
		List<Conteudo> conteudos = new ArrayList();
		session = Conexao.getSession();
		try {
			session.beginTransaction();
			conteudos = session.createCriteria(Conteudo.class).list();
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return conteudos;
	}
	
	public Conteudo getById(int id) {
		Conteudo conteudo = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			conteudo = (Conteudo) session.get(Conteudo.class, id);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return conteudo;
	}
	
	public Conteudo buscar(Conteudo conteudoBusca) {
		Conteudo conteudo = null;
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			
			String select = "from Conteudo as u "
					+ "where u.titulo = :titulo";
			
			Query query = session.createQuery(select)
					.setParameter("titulo", conteudoBusca.getTitulo());
			
			List<?> resultList = query.list();
			for(Object obj : resultList) {
				Object grupo = (Object) obj;
				conteudo = (Conteudo) grupo;
			}
			
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return conteudo;
	}
	
}


